<template>
  <div
    v-loading="loading"
    class="home-container"
    ref="home"
    @wheel="handleWheel"
  >
    <ul
      class="homeItem"
      :style="{ marginTop }"
      @transitionend="isScrool = false"
    >
      <li v-for="item in data" :key="item.id">
        <Carouselitme :carousel="item" />
      </li>
    </ul>
    <div class="icon iconUp" @click="change(index - 1)" v-show="index > 0">
      <Icon type="arrowUp"></Icon>
    </div>
    <div
      class="icon iconDown"
      @click="change(index + 1)"
      v-show="index < data.length - 1"
    >
      <Icon type="arrowDown"></Icon>
    </div>
    <ul class="indicator">
      <li
        v-for="(item, i) in data"
        @click="change(i)"
        :class="{ active: index == i }"
        :key="item.id"
      ></li>
    </ul>
  </div>
</template>

<script>
import Carouselitme from "./Carouselitme.vue";
import Icon from "@/components/Icon";
import { getBanner } from "@/api/banner";
import { mapState } from "vuex";
export default {
  data() {
    return {
      index: 0, //当前显示的是第几张轮播图
      containerHeight: 0,
      isScrool: false,
    };
  },
  created() {
    this.$store.dispatch("banner/fetchData");
  },
  mounted() {
    this.containerHeight = this.$refs.home.clientHeight;
    window.addEventListener("resize", this.handleResize);
  },
  destroyed() {
    window.removeEventListener("resize", this.handleResize);
  },
  components: {
    Carouselitme,
    Icon,
  },
  computed: {
    marginTop() {
      return -this.index * this.containerHeight + "px";
    },
    ...mapState("banner", ["loading", "data"]),
    // loading(){
    //     return this.$store.state.banner.loading;
    // },
    // data(){
    //     console.log(this.$store.state.data)
    //     return this.$store.state.banner.data;
    // }
  },
  methods: {
    change(i) {
      this.index = i;
    },
    handleWheel(e) {
      if (this.isScrool) {
        return;
      }
      if (e.deltaY > 0 && this.index < this.data.length - 1) {
        this.isScrool = true;
        this.index++;
      } else if (e.deltaY < 0 && this.index > 0) {
        this.isScrool = true;
        this.index--;
      }
    },
    handleResize() {
      this.containerHeight = this.$refs.home.clientHeight;
    },
  },
};
</script>

<style lang="less" scoped>
@import "~@/style/var.less";
@import "~@/style/mixin.less";
.home-container {
  width: 100%;
  height: 100%;
  position: relative;
  color: #fff;
  overflow: hidden;
  .homeItem {
    transition: all 0.4s;
    width: 100%;
    height: 100%;
    list-style: none;
    margin: 0;
    padding: 0;
    li {
      // overflow: hidden;
      // background-color: @dark;
      width: 100%;
      height: 100%;
      box-sizing: border-box;
    }
  }

  .indicator {
    .self-center();
    left: auto;
    right: 15px;
    // transform: translate(-50%, -50%);
    li {
      list-style: none;
      width: 7px;
      height: 7px;
      border: 2px solid #fff;
      border-radius: 50%;
      margin-bottom: 8px;
      cursor: pointer;
      &.active {
        background: #fff;
      }
    }
  }
}
.icon {
  .self-center();
  font-size: 26px;
  color: @gray;
  cursor: pointer;
  &Up {
    top: 20px;
    transform: translateX(-50%);
    animation: jumpUp 1.2s infinite;
  }
  &Down {
    top: auto;
    bottom: 20px;
    animation: jumpDown 1.2s infinite;
  }
}
@keyframes jumpUp {
  0% {
    top: 20px;
  }
  50% {
    top: 12px;
  }

  100% {
    top: 20px;
  }
}
@keyframes jumpDown {
  0% {
    bottom: 20px;
  }
  50% {
    bottom: 12px;
  }

  100% {
    bottom: 20px;
  }
}
</style>